#include "chatcli.h"
#include "ui_chatcli.h"

Chatcli::Chatcli(QTcpSocket *s, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Chatcli)
{
    ui->setupUi(this);
    client = s;

    connect(client,&QTcpSocket::readyRead,this,&Chatcli::response);

}

Chatcli::~Chatcli()
{
    delete ui;
}


void Chatcli::on_clearButton_clicked()
{
    ui->lineEdit->clear();
}


void Chatcli::response()
{
    QByteArray responseData;
//    while (client->waitForReadyRead(500)) { // 循环等待响应数据
//        responseData += client->readAll();
//        qDebug()<<"客户端处理数据中";
//    }
    responseData += client->readAll();
    qDebug() << "Received response:" << QString(responseData);
    ui->lineEdit_2->setText(QString(responseData));
}

void Chatcli::on_sendButton_clicked()
{
    QByteArray ba;
    ba.append(ui->lineEdit->text().toUtf8());
    qDebug()<<"     current main thread ID:"<<QThread::currentThreadId()<<'\n';
    Mythread *t = new Mythread(client,ba);
    t->start();

    QByteArray responseData;
//    while (client->waitForReadyRead(500)) { // 循环等待响应数据
//        responseData += client->readAll();
//        qDebug()<<"客户端处理数据中";
//    }
    responseData += client->readAll();
    qDebug() << "Received response:" << QString(responseData);
    ui->lineEdit_2->setText(QString(responseData));
}

